import threading
import time
from tkinter import simpledialog
import uuid
import pyautogui
import sys
import mwindow
from pynput.keyboard import Controller, Key, Listener
import win32gui
import win32con
from enum import Enum
import trainee
from scores.scorea import CalcA
from traineeskill import ts
import operate

def on_press(key):
    # try:
    #     print("正在按压:", format(key.char))
    # except AttributeError:
    #     print("正在按压2:", format(key))
    if(key.char == 'q'):
        return False
    
def ontimer(gdata: operate.Operate):
    gdata.PrintTime()
    gdata.DoMain()
    loop_monitor(gdata)
def loop_monitor(gdata):
    timer=threading.Timer(1,ontimer,args=[gdata])
    timer.start()    

def GetMainWnd():
    hwnd = win32gui.FindWindow('Chrome_WidgetWin_0', '国王指意')    
    if hwnd == None:
        print("未检测到国王指意窗体")
        return False
    windows = pyautogui.getWindowsWithTitle("国王指意")
    mainwin = None
    for w in windows:
        if w._hWnd == hwnd:
            mainwin = w
            break
    return mainwin

if __name__ == "__main__":
    pyautogui.PAUSE = 2.5
    pyautogui.FAILSAFE = True
    print("start")
    user_input = simpledialog.askinteger("输入数字", "请输入学徒个数, 如果填0, 则自动判断结束: ")
    mainwin = GetMainWnd()
    if mainwin == None:
        print("没有找到")
        sys.exit(0)
    mainwin.move(0,0)
    # win32gui.BringWindowToTop(hwnd)
    mainwin.activate()
    # print("6")
    if mainwin.box.left < -32000:
        print("窗口小于-30000")
        sys.exit(0)

    # scorea.ScoreA(ts,None)
    # 初始化窗口集合
    mset = mwindow.MWindowSet((mainwin.box.left,mainwin.box.top,mainwin.box.width,mainwin.box.height))
    mset.Load('分析学徒.mset')

    # with Listener(on_press=on_press) as listener:
    #     listener.join()

    gdata = operate.Operate(mset,user_input)
    loop_monitor(gdata)
